package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

//4ms 33%, 2.4MB 99%
func reverseList(head *ListNode) *ListNode {
	var pre *ListNode
	for head != nil {
		next := head.Next
		head.Next = pre
		pre = head
		head = next
	}
	return pre
}

func main() {
	list := &ListNode{1, nil}
	head := list
	for i := 2; i < 6; i++ {
		list.Next = &ListNode{i, nil}
		list = list.Next
		fmt.Println(head, list)
	}
	fmt.Println(reverseList(head))
}
